package com.api.gbhg.xingyuhao.service;
import com.alibaba.fastjson.JSON;
import com.api.gbhg.daizhishun.Vo.ExpenseAccountVo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xignyuhao
 */
public class PoiServiceImpl   {

    /**
     * 浏览器导出
     * @param response
     * @param data
     */
    public String downLoadExcel1(HttpServletResponse response, List<ExpenseAccountVo> data)  {

        final Logger logger = LoggerFactory.getLogger(PoiServiceImpl.class);

        Map<String, String> map=new HashMap<>();

        logger.info("前端传入的数据 >>："+data.toString());

        try {

            String[] headers = {"报销单编号", "报销日期", "经办人", "预算来源", "预算项目", "关联号", "用途", "贷方金额", "收款人"};

            //headers表示excel表中第一行的表头
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("sheet2");


            //新增数据行，并且设置单元格数据
            HSSFRow row = sheet.createRow(0);
            //定义单元格风格
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            //设置水平居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            //设置垂直居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置字体
            HSSFFont font = workbook.createFont();
            //设置字体名称
            font.setFontName("华文行楷");
            //设置字号
            font.setFontHeightInPoints((short)15);
            //设置是否为斜体
            font.setItalic(false);
            //设置是否加粗
            font.setBold(false);
            //设置字体颜色
            font.setColor(IndexedColors.BLACK.index);
            //设置行的高度
            cellStyle.setFont(font);
            row.setHeightInPoints(25);
            //设置默认的列宽度
            sheet.setDefaultColumnWidth(26);


            //在excel表中添加表头
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell(i);
                //渲染单元格
                cell.setCellStyle(cellStyle);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text);
            }

            int rowNum = 1;


            //在表中存放查询到的数据放入对应的列
            for (ExpenseAccountVo data1 : data) {

                HSSFRow row1 = sheet.createRow(rowNum);

                row1.createCell(0).setCellValue(data1.getLcbh());
                row1.getCell(0).setCellStyle(cellStyle);


                row1.createCell(1).setCellValue(data1.getSqrq());
                row1.getCell(1).setCellStyle(cellStyle);

                row1.createCell(2).setCellValue(data1.getJbr());
                row1.getCell(2).setCellStyle(cellStyle);

                row1.createCell(3).setCellValue(data1.getYsly());
                row1.getCell(3).setCellStyle(cellStyle);

                row1.createCell(4).setCellValue(data1.getYsxm());
                row1.getCell(4).setCellStyle(cellStyle);

                row1.createCell(5).setCellValue(data1.getGlh());
                row1.getCell(5).setCellStyle(cellStyle);

                row1.createCell(6).setCellValue(data1.getBmjjzcfl());
                row1.getCell(6).setCellStyle(cellStyle);

                row1.createCell(7).setCellValue(data1.getSqbxje());
                row1.getCell(7).setCellStyle(cellStyle);

                row1.createCell(8).setCellValue(data1.getSkr());
                row1.getCell(8).setCellStyle(cellStyle);


                logger.info("放入cell数据 报销单编号："+data1.getJbr()+"经办人："+data1.getJbr());

                //设置行的高度
                row1.setHeightInPoints(38);

                rowNum++;
            }

            //设置要导出的文件的名字
            String downFilename  = "info"  + ".xls";
            //服务器发给浏览器的数据是使用UTF-8编码
            response.setContentType("application/octet-stream ; charset=ISO-8859-1 ");

            //设置浏览器的编码为UTF-8
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(downFilename,"UTF-8"));


            response.flushBuffer();
            workbook.write(response.getOutputStream());

            map.put("导出成功","ok");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("导出失败","error");
        }
        return JSON.toJSONString(map);
    }
}